package movieGandalf.GUI;

import movieGandalf.GUI.GUIManager.StatusEnum;
import movieGandalf.common.Log;
import movieGandalf.database.MovieTools;
import movieGandalf.logicGuiConnection.Genre;
import movieGandalf.logicGuiConnection.Movie;
import movieGandalf.logicGuiConnection.Role;
import movieGandalf.logicGuiConnection.User;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


public class MoviePage extends Composite {
	
	/* The different types of user lists */
	private enum UserListEnum {
		WATCH,
		SEEN,
		MUST_SEE;
	}

	private Movie movie;
	private User user;
	
	private Button btnSeen;
	private Button btnMustSee;
	private Button btnWatch;

	private static final int X_MARGIN = 25;
	private static final int Y_MARGIN = 10;
	private static final int TEXT_FIELD_WIDTH = 260;
	private static final int LABEL_Y_SPACING = 20;
	private static final int LABEL_TEXT_DIFF = -3;
	private static final int LABEL_TEXT_SPACING = 50;
	
	
	UserListEnum listToUpdate;
	boolean result;


	public MoviePage(Composite parent, int style,
			Movie movie, User user) {
		super(parent, style);
		
		this.user=user;
		this.movie = movie;
		createMoviePage();
	}

	private void createMoviePage() {

		this.setLayout(new FormLayout());
		this.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
	    

		Label lblGenre = new Label(this, SWT.NONE);
		lblGenre.setText("Genre:");
		FormData fd_lblGenre = new FormData();
		fd_lblGenre.top = new FormAttachment(0, Y_MARGIN);
		fd_lblGenre.left = new FormAttachment(0, X_MARGIN);
		lblGenre.setLayoutData(fd_lblGenre);

		final Text genreSearchText = new Text(this, SWT.BORDER);
		FormData fd_genreSearchText = new FormData();
		fd_genreSearchText.top = new FormAttachment(lblGenre, LABEL_TEXT_DIFF, SWT.TOP);
		fd_genreSearchText.width = TEXT_FIELD_WIDTH;
		fd_genreSearchText.left = new FormAttachment(lblGenre, LABEL_TEXT_SPACING);
		genreSearchText.setLayoutData(fd_genreSearchText);
		genreSearchText.setEditable(false);
		
	

		Label lblActors = new Label(this, SWT.NONE);
		lblActors.setText("Actors:");
		FormData fd_lblActors = new FormData();
		fd_lblActors.top = new FormAttachment(lblGenre, LABEL_Y_SPACING);
		fd_lblActors.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblActors.setLayoutData(fd_lblActors);
		
		final Text actorsSearchText = new Text(this, SWT.BORDER);
		FormData fd_ActorsSearch = new FormData();
		fd_ActorsSearch.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_ActorsSearch.top = new FormAttachment(lblActors, LABEL_TEXT_DIFF, SWT.TOP);
		fd_ActorsSearch.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		actorsSearchText.setLayoutData(fd_ActorsSearch);
		actorsSearchText.setEditable(false);
		
		
		
		Label lblYear = new Label(this, SWT.NONE);
		lblYear.setText("Year:");
		FormData fd_lblYear = new FormData();
		fd_lblYear.top = new FormAttachment(lblActors, LABEL_Y_SPACING);
		fd_lblYear.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblYear.setLayoutData(fd_lblYear);

		final Text minYearSearch = new Text(this, SWT.BORDER);
		FormData fd_minYearSearch = new FormData();
		fd_minYearSearch.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_minYearSearch.top = new FormAttachment(lblYear, LABEL_TEXT_DIFF, SWT.TOP);
		fd_minYearSearch.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		minYearSearch.setLayoutData(fd_minYearSearch);
		minYearSearch.setEditable(false);
		
	
		Label lblLanguage = new Label(this, SWT.NONE);
		FormData fd_lblLanguage = new FormData();
		fd_lblLanguage.top = new FormAttachment(lblYear, LABEL_Y_SPACING);
		fd_lblLanguage.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblLanguage.setLayoutData(fd_lblLanguage);
		lblLanguage.setText("Language:");

		final Text languageSearchText = new Text(this, SWT.BORDER);
		FormData fd_langSearchText = new FormData();
		fd_langSearchText.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_langSearchText.top = new FormAttachment(lblLanguage, LABEL_TEXT_DIFF, SWT.TOP);
		fd_langSearchText.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		languageSearchText.setLayoutData(fd_langSearchText);
		languageSearchText.setEditable(false);
		


		Label lblColor = new Label(this, SWT.NONE);
		lblColor.setText("Color/BW:");
		FormData fd_lblColor = new FormData();
		fd_lblColor.top = new FormAttachment(lblLanguage, LABEL_Y_SPACING);
		fd_lblColor.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblColor.setLayoutData(fd_lblColor);
		
		final Text colorSearchText = new Text(this, SWT.BORDER);
		FormData fd_colorSearchText = new FormData();
		fd_colorSearchText.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_colorSearchText.top = new FormAttachment(lblColor, LABEL_TEXT_DIFF, SWT.TOP);
		fd_colorSearchText.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		colorSearchText.setLayoutData(fd_colorSearchText);
		colorSearchText.setEditable(false);


		
		Label lblLength = new Label(this, SWT.NONE);
		lblLength.setText("Length:");
		FormData fd_lblLength = new FormData();
		fd_lblLength.top = new FormAttachment(lblColor, LABEL_Y_SPACING);
		fd_lblLength.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblLength.setLayoutData(fd_lblLength);
		
		final Text LengthSearchText = new Text(this, SWT.BORDER);
		FormData fd_LengthSearchText = new FormData();
		fd_LengthSearchText.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_LengthSearchText.top = new FormAttachment(lblLength, LABEL_TEXT_DIFF, SWT.TOP);
		fd_LengthSearchText.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		LengthSearchText.setLayoutData(fd_LengthSearchText);
		LengthSearchText.setEditable(false);


		
		Label lblRank = new Label(this, SWT.NONE);
		lblRank.setText("Rank:");
		FormData fd_lblRank = new FormData();
		fd_lblRank.top = new FormAttachment(lblLength, LABEL_Y_SPACING);
		fd_lblRank.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblRank.setLayoutData(fd_lblRank);
		
		final Text rankSearchText = new Text(this, SWT.BORDER);
		FormData fd_rankSearchText = new FormData();
		fd_rankSearchText.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_rankSearchText.top = new FormAttachment(lblRank, LABEL_TEXT_DIFF, SWT.TOP);
		fd_rankSearchText.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		rankSearchText.setLayoutData(fd_rankSearchText);
		rankSearchText.setEditable(false);

		
		Label lblDirector = new Label(this, SWT.NONE);
		FormData fd_lblDirector = new FormData();
		fd_lblDirector.top = new FormAttachment(lblRank, LABEL_Y_SPACING);
		fd_lblDirector.left = new FormAttachment(lblGenre, 0, SWT.LEFT);
		lblDirector.setLayoutData(fd_lblDirector);
		lblDirector.setText("Director:");
		
		final Text directorSearchText = new Text(this, SWT.BORDER);
		FormData fd_directorSearchText = new FormData();
		fd_directorSearchText.right = new FormAttachment(genreSearchText, 0, SWT.RIGHT);
		fd_directorSearchText.top = new FormAttachment(lblDirector, LABEL_TEXT_DIFF, SWT.TOP);
		fd_directorSearchText.left = new FormAttachment(genreSearchText, 0, SWT.LEFT);
		directorSearchText.setLayoutData(fd_directorSearchText);
		directorSearchText.setEditable(false);

		
		Text countrySearchText = new Text(this, SWT.BORDER);
		FormData fd_countrySearchText = new FormData();
		fd_countrySearchText.width = TEXT_FIELD_WIDTH;
		fd_countrySearchText.top = new FormAttachment(genreSearchText, 0, SWT.TOP);
		fd_countrySearchText.right = new FormAttachment(100, -X_MARGIN);
		countrySearchText.setLayoutData(fd_countrySearchText);
		countrySearchText.setEditable(false);

		
		Label lblCountry = new Label(this, SWT.NONE);
		lblCountry.setText("Country:");
		FormData fd_lblCountry = new FormData();
		fd_lblCountry.top = new FormAttachment(lblGenre, 0, SWT.TOP);
		fd_lblCountry.right = new FormAttachment(countrySearchText, -LABEL_TEXT_SPACING);
		lblCountry.setLayoutData(fd_lblCountry);

		
		Label lblMPAARating = new Label(this, SWT.NONE);
		lblMPAARating.setText("MPAA Rating:");
		FormData fd_lblMPAARating = new FormData();
		fd_lblMPAARating.top = new FormAttachment(lblActors, 0, SWT.TOP);
		fd_lblMPAARating.left = new FormAttachment(lblCountry, 0, SWT.LEFT);
		lblMPAARating.setLayoutData(fd_lblMPAARating);
		

		Text mpaaRatingSearchText = new Text(this, SWT.BORDER);
		FormData fd_mpaaRatingSearchText = new FormData();
		fd_mpaaRatingSearchText.right = new FormAttachment(countrySearchText, 0, SWT.RIGHT);
		fd_mpaaRatingSearchText.top = new FormAttachment(lblMPAARating, LABEL_TEXT_DIFF, SWT.TOP);
		fd_mpaaRatingSearchText.left = new FormAttachment(countrySearchText, 0, SWT.LEFT);
		mpaaRatingSearchText.setLayoutData(fd_mpaaRatingSearchText);
		mpaaRatingSearchText.setEditable(false);


		
		Label lblOurRating = new Label(this, SWT.NONE);
		FormData fd_lblOurRating = new FormData();
		fd_lblOurRating.top = new FormAttachment(lblYear, 0, SWT.TOP);
		fd_lblOurRating.left = new FormAttachment(lblCountry, 0, SWT.LEFT);
		lblOurRating.setLayoutData(fd_lblOurRating);
		lblOurRating.setText("Our Rating:");

		
		Text ourRatingSearchText = new Text(this, SWT.BORDER);
		FormData fd_ourRatingSearchText = new FormData();
		fd_ourRatingSearchText.right = new FormAttachment(countrySearchText, 0, SWT.RIGHT);
		fd_ourRatingSearchText.top = new FormAttachment(lblOurRating, LABEL_TEXT_DIFF, SWT.TOP);
		fd_ourRatingSearchText.left = new FormAttachment(countrySearchText, 0, SWT.LEFT);
		ourRatingSearchText.setLayoutData(fd_ourRatingSearchText);
		ourRatingSearchText.setEditable(false);



		Label lblFreeText = new Label(this, SWT.NONE);
		lblFreeText.setText("Description:");
		FormData fd_lblFreeText = new FormData();
		fd_lblFreeText.top = new FormAttachment(lblLanguage, 0, SWT.TOP);
		fd_lblFreeText.left = new FormAttachment(lblCountry, 0, SWT.LEFT);
		lblFreeText.setLayoutData(fd_lblFreeText);

		Text FreeTextSearch = new Text(this, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		FormData fd_FreeTextSearch = new FormData();
		fd_FreeTextSearch.bottom = new FormAttachment(directorSearchText, 0, SWT.BOTTOM);
		fd_FreeTextSearch.right = new FormAttachment(countrySearchText, 0, SWT.RIGHT);
		fd_FreeTextSearch.top = new FormAttachment(lblFreeText, LABEL_TEXT_DIFF, SWT.TOP);
		fd_FreeTextSearch.left = new FormAttachment(countrySearchText, 0, SWT.LEFT);
		FreeTextSearch.setLayoutData(fd_FreeTextSearch);
		FreeTextSearch.setEditable(false);

		
		btnWatch = new Button(this, SWT.PUSH);
		FormData fd_btnWatch = new FormData();
		fd_btnWatch.top = new FormAttachment(FreeTextSearch, LABEL_Y_SPACING);
		fd_btnWatch.right = new FormAttachment(countrySearchText, 0, SWT.RIGHT);
		fd_btnWatch.left = new FormAttachment(countrySearchText, 0, SWT.LEFT);
		
		btnWatch.setLayoutData(fd_btnWatch);
		btnWatch.setText("Add to Watch List");
		
		btnSeen = new Button(this, SWT.PUSH);
		FormData fd_btnSeen = new FormData();
		fd_btnSeen.top = new FormAttachment(btnWatch, LABEL_Y_SPACING);
		fd_btnSeen.right = new FormAttachment(countrySearchText, 0, SWT.RIGHT);
		fd_btnSeen.left = new FormAttachment(countrySearchText, 0, SWT.LEFT);
		
		btnSeen.setLayoutData(fd_btnSeen);
		btnSeen.setText("Add to Already Seen List");
		
		
		btnMustSee = new Button(this, SWT.PUSH);
		FormData fd_btnMustSee = new FormData();
		fd_btnMustSee.top = new FormAttachment(btnSeen, LABEL_Y_SPACING);
		fd_btnMustSee.right = new FormAttachment(countrySearchText, 0, SWT.RIGHT);
		fd_btnMustSee.left = new FormAttachment(countrySearchText, 0, SWT.LEFT);
		
		
		btnMustSee.setLayoutData(fd_btnMustSee);
		btnMustSee.setText("Add to Must See List");

		
		
		btnWatch.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				btnWatch.setEnabled(false);
				sendParams(UserListEnum.WATCH);
			}
		});
		
		
		btnMustSee.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				btnMustSee.setEnabled(false);
				sendParams(UserListEnum.MUST_SEE);
			}
		});
		
		
		btnSeen.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				btnSeen.setEnabled(false);
				sendParams(UserListEnum.SEEN);
			}
		});
		
		
		//fields population
		try{
		    String genre = "";
		    for (Genre g: movie.getGenres())
		    {
		    	if (genre.length()==0)
		    		genre = g.getGenre();	    	
		    	else
		    	genre= genre + ", " + g.getGenre();
		    }

		    
			String actor = "";
		    for (Role r: movie.getActorList())
		    {
		    	if (actor.length()==0)
		    		actor = r.getActor().getName();	    	
		    	else
		    		actor = actor + ", " + r.getActor().getName();
		    }
			
		    genreSearchText.setText(genre);
		    actorsSearchText.setText(actor);
		    minYearSearch.setText(Integer.toString(movie.getYear()));
			countrySearchText.setText(movie.getCountryName());
			languageSearchText.setText(movie.getLanguageName());
			colorSearchText.setText(movie.isColor() ? "Color" : "B/W");
			LengthSearchText.setText(Integer.toString(movie.getLength()));
			rankSearchText.setText(String.format("%.1f", movie.getOurRating()));
			directorSearchText.setText(movie.getDirector().getName());
			mpaaRatingSearchText.setText(movie.getMPAARatingName());
			ourRatingSearchText.setText(String.format("%.1f",movie.getOurRating()));
			FreeTextSearch.setText(movie.getDescription());
		}catch (Exception e)
		{
			Log.getInstance().WriteMessage("Movie isn't full", e);
			GUIManager.sendStatus("Error loading Movie details", StatusEnum.ERROR);
		}

		
		
		this.layout(true);
		this.pack();
	}
	
		
		// Sends parameters to BL add to user list
		private void sendParams(UserListEnum listType) {
			listToUpdate=listType;
			
			Thread newThread = new Thread() {
				public void run() {
					getResult();
				}
			};
			newThread.start();
		}

		// Getting result from update func
		private void getResult() {
			switch (listToUpdate)
			{
			case MUST_SEE:
				try {
					//TODO: fix this if incorrect
					if (MovieTools.addWishList(MovieTools.getConn(), user.getID(), movie.getID()))
						
						GUIManager.sendStatus("Movie added to must see list successfully",
							StatusEnum.INFORM);					
					
				} catch (Exception e) {
					Log.getInstance().WriteMessage("Error adding to must see list", e);
					GUIManager.sendStatus("Error adding to must see list", StatusEnum.ERROR);
				} 				
				break;
			
			
			case WATCH:
				try {
					//TODO: fix this if incorrect
					if (MovieTools.addAlreadySeen(MovieTools.getConn(), user.getID(), movie.getID()))
						
						GUIManager.sendStatus("Movie added to watch list successfully",
							StatusEnum.INFORM);
										
				} catch (Exception e) {
					Log.getInstance().WriteMessage("Error adding to watch see list", e);
					GUIManager.sendStatus("Error adding to watch list", StatusEnum.ERROR);
				} 				
				break;
		
			
			case SEEN:
				try {
					//TODO: fix this if incorrect
					if (MovieTools.addAlreadySeen(MovieTools.getConn(), user.getID(), movie.getID()))
						
						GUIManager.sendStatus("Movie added to seen list successfully",
							StatusEnum.INFORM);				
					
				} catch (Exception e) {
					Log.getInstance().WriteMessage("Error adding to seen list", e);
					GUIManager.sendStatus("Error adding to seen list", StatusEnum.ERROR);
				} 				
				break;
			
			default:
				GUIManager.sendStatus("Error adding to list", StatusEnum.ERROR);
			}
			
			Runnable run = new Runnable() {

				@Override
				public void run() {
					releaseButton();
				}
			};

			GUIManager.sendToMainThread(run);
	}

		private void releaseButton()
		{
			switch (listToUpdate)
			{
			case MUST_SEE:
				btnMustSee.setEnabled(true);
				break;			
			case WATCH:	
				btnWatch.setEnabled(true);
				break;			
			case SEEN:	
				btnSeen.setEnabled(true);
				break;
			}			
		}
}
